﻿using CommunityToolkit.Mvvm.ComponentModel;
using DataCollection.Core.Models;
using DataCollection.Infrastructure;
using Mapster;

namespace DataCollection.WinForm.ViewModel
{
    internal partial class SystemSetViewModel : ObservableObject
    {
        #region 属性
        private readonly SystemSetModel model = new SystemSetModel();

        [ObservableProperty]
        private int number;

        [ObservableProperty]
        private string voicePortAddress = string.Empty;

        [ObservableProperty]
        private string voiceBaudrate = string.Empty;

        [ObservableProperty]
        private int voiceEnable;

        [ObservableProperty]
        private int broadcastEnable;

        [ObservableProperty]
        private string broadcastInterval = string.Empty;

        [ObservableProperty]
        private string holdingTime = string.Empty;

        [ObservableProperty]
        private string holdingPointCount = string.Empty;

        [ObservableProperty]
        private string pressureScaleMultiple = string.Empty;

        [ObservableProperty]
        private string temperatureScaleMultiple = string.Empty;

        #endregion

        public SystemSetViewModel(int number)
        {
            Dictionary<string, object> where = new Dictionary<string, object>();
            where["number"] = number.ToString();
            List<SystemSetModel> list = SystemSetModel.List(where);
            if (list.Count > 0)
            {
                this.model = list.First<SystemSetModel>();
                this.model.Adapt(this);
            }
            else
            {
                this.number = number;
            }
        }

        public void Apply()
        {
            this.Adapt(this.model);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        public Response Update()
        {
            Response response = Response.DefaultResponse();
            int res = this.model.Update();
            if (res == 1)
            {
                response.Code = Response.SuccessCode;
                response.Msg = "编辑成功";
                Singleton.Instance.SetModel = model;
            }
            else
            {
                response.Msg = "编辑失败";
            }
            return response;
        }
    }
}
